
#ifndef PACK_H_FILE
#define PACK_H_FILE

#define PACK_INLINE __inline__ static

PACK_INLINE void pack_u8_le(void *buf, unsigned char n)
{
  unsigned char *b = buf;
  b[0] = n;
}

PACK_INLINE void pack_u16_le(void *buf, unsigned short n)
{
  unsigned char *b = buf;
  b[0] = n & 0xff;
  b[1] = (n >> 8) & 0xff;
}

PACK_INLINE void pack_u32_le(void *buf, unsigned int n)
{
  unsigned char *b = buf;
  b[0] = n & 0xff;
  b[1] = (n >>  8) & 0xff;
  b[2] = (n >> 16) & 0xff;
  b[3] = (n >> 24) & 0xff;
}

PACK_INLINE void pack_u64_le(void *buf, unsigned long long n)
{
  unsigned char *b = buf;
  b[0] = n & 0xff;
  b[1] = (n >>  8) & 0xff;
  b[2] = (n >> 16) & 0xff;
  b[3] = (n >> 24) & 0xff;
  b[4] = (n >> 32) & 0xff;
  b[5] = (n >> 40) & 0xff;
  b[6] = (n >> 48) & 0xff;
  b[7] = (n >> 56) & 0xff;
}

PACK_INLINE void pack_u32_be(void *buf, unsigned int n)
{
  unsigned char *b = buf;
  b[0] = (n >> 24) & 0xff;
  b[1] = (n >> 16) & 0xff;
  b[2] = (n >>  8) & 0xff;
  b[3] = n & 0xff;
}

PACK_INLINE void pack_u64_be(void *buf, unsigned long long n)
{
  unsigned char *b = buf;
  b[0] = (n >> 56) & 0xff;
  b[1] = (n >> 48) & 0xff;
  b[2] = (n >> 40) & 0xff;
  b[3] = (n >> 32) & 0xff;
  b[4] = (n >> 24) & 0xff;
  b[5] = (n >> 16) & 0xff;
  b[6] = (n >>  8) & 0xff;
  b[7] = n & 0xff;
}

PACK_INLINE unsigned int unpack_u32_be(void *buf)
{
  unsigned char *b = buf;
  unsigned int n;

  n = b[0];
  n = (n << 8) | b[1];
  n = (n << 8) | b[2];
  n = (n << 8) | b[3];
  return n;
}

PACK_INLINE unsigned long long unpack_u64_be(void *buf)
{
  unsigned char *b = buf;
  unsigned long long n;

  n = b[0];
  n = (n << 8) | b[1];
  n = (n << 8) | b[2];
  n = (n << 8) | b[3];
  n = (n << 8) | b[4];
  n = (n << 8) | b[5];
  n = (n << 8) | b[6];
  n = (n << 8) | b[7];
  return n;
}

PACK_INLINE unsigned int unpack_u32_le(void *buf)
{
  unsigned char *b = buf;
  unsigned int n;

  n = b[3];
  n = (n << 8) | b[2];
  n = (n << 8) | b[1];
  n = (n << 8) | b[0];
  return n;
 
}

PACK_INLINE unsigned short unpack_u16_le(void *buf)
{
  unsigned char *b = buf;
  unsigned short n;

  n = b[1];
  n = (n << 8) | b[0];
  return n;
 
}

PACK_INLINE unsigned char unpack_u8_le(void *buf)
{
  unsigned char *b = buf;
  return b[0];
 
}

#endif /* PACK_H_FILE */
